﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Exp.DAL
Imports Exp.Model
Public Class Unit_Service
    Public Function GetAll() As List(Of REM_Unit)
        Dim ctx As New EXPEntities
        Dim list = ctx.REM_Unit.ToList()
        Return list
    End Function
    Public Function GetAllData() As List(Of EXP_Select_REM_Unit_And_REM_Resort_Result)
        Dim ctx As New EXPEntities
        Dim list = ctx.EXP_Select_REM_Unit_And_REM_Resort().ToList()
        Return list
    End Function

    Public Function GetAllUnit_NotInOwner() As List(Of EXP_Select_REM_Unit_NotInOwner_Result)
        Dim ctx As New EXPEntities
        Dim list = ctx.EXP_Select_REM_Unit_NotInOwner.ToList()
        Return list
    End Function

    Public Function GetAllDataTest() As List(Of REM_Unit)
        Dim ctx As New EXPEntities
        Dim list = ctx.REM_Unit.Include("REM_UnitType").Include("REM_Resort").ToList()

        Return list
    End Function
    Public Function GetByName(ByVal sUnitName As String) As List(Of REM_Unit)
        Dim ctx As New EXPEntities
        Dim lstObj As New List(Of REM_Unit)
        lstObj = ctx.REM_Unit.Where(Function(o) o.UnitName = sUnitName).ToList()
        Return lstObj
    End Function
    Public Function GetByID(ByVal ID As Integer) As REM_Unit
        Dim ctx As New EXPEntities
        Dim obj = ctx.REM_Unit.Where(Function(o) o.Unit_ID = ID).FirstOrDefault()
        Return obj
    End Function
    Public Sub Add(objREM_Unit As REM_Unit)
        Dim ctx As New EXPEntities
        ctx.REM_Unit.AddObject(objREM_Unit)
        ctx.SaveChanges()
    End Sub
    Public Sub Delete(ByVal iUnitID As Integer)
        Dim ctx As New EXPEntities
        Dim obj = ctx.REM_Unit.Where(Function(o) o.Unit_ID = iUnitID).FirstOrDefault()
        ctx.REM_Unit.DeleteObject(obj)
        ctx.SaveChanges()
    End Sub
    Public Sub Edit(objREM_Unit As REM_Unit)
        Dim ctx As New EXPEntities
        Dim obj = ctx.REM_Unit.Where(Function(o) o.Unit_ID = objREM_Unit.Unit_ID).FirstOrDefault()
        obj.Unit_ID = objREM_Unit.UnitName
        obj.UpdateDate = objREM_Unit.UpdateDate
        obj.UpdateUser = objREM_Unit.UpdateUser
        'obj.CreateUser = objREM_UnitType.CreateUser
        'obj.CreateDate = objREM_UnitType.CreateDate
        ctx.SaveChanges()
    End Sub
    Public Sub Update(objREM_Unit As REM_Unit)
        Dim ctx As New EXPEntities
        Dim obj = ctx.REM_Unit.Where(Function(o) o.Unit_ID = objREM_Unit.Unit_ID).FirstOrDefault()
        obj.UnitName = objREM_Unit.UnitName
        obj.Resort_ID = objREM_Unit.Resort_ID
        obj.UnitType_ID = objREM_Unit.UnitType_ID
        obj.RatePrice = objREM_Unit.RatePrice
        obj.Sqm = objREM_Unit.Sqm
        obj.PointPerNight = objREM_Unit.PointPerNight
        obj.Currency_ID = objREM_Unit.Currency_ID
        obj.Address = objREM_Unit.Address
        obj.Description = objREM_Unit.Description
        obj.isActive = objREM_Unit.isActive
        obj.UpdateDate = objREM_Unit.UpdateDate
        obj.UpdateUser = objREM_Unit.UpdateUser
        ctx.SaveChanges()
    End Sub
    Public Function GetLastID() As Integer
        Dim ctx As New EXPEntities
        Dim iReturn As Integer = 0
        Dim lstObj As New List(Of REM_Unit)
        lstObj = ctx.REM_Unit.ToList()
        lstObj = ctx.REM_Unit.OrderByDescending(Function(p) p.Unit_ID).ToList()
        If Not lstObj Is Nothing Then
            If lstObj.Count > 0 Then
                iReturn = lstObj.Item(0).Unit_ID + 1
            Else
                iReturn = 1
            End If
        End If
        Return iReturn
    End Function
    Public Function GetEXP_Select_RET_Booking_And_REM_Unit(iMonth As Integer, iYear As Integer) As List(Of EXP_Select_RET_Booking_And_REM_Unit_Result)
        Dim ctx As New EXPEntities
        Dim iReturn As Integer = 0
        Dim lstObj As New List(Of EXP_Select_RET_Booking_And_REM_Unit_Result)
        lstObj = ctx.EXP_Select_RET_Booking_And_REM_Unit(iMonth, iYear).ToList()
        lstObj = lstObj.OrderByDescending(Function(p) p.Unit_ID).ToList()
        If Not lstObj Is Nothing Then
            If lstObj.Count > 0 Then
                Return lstObj
            End If
        End If
        Return lstObj
    End Function
End Class
